//
//  ZTListViewController.m
//  XMLYFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "ZTListViewController.h"
#import "ZTListModel.h"
#import "ZTListTableViewCell.h"
#import "AppTools.h"
#import <UIImageView+WebCache.h>
#import "ZTListDetailViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ZTListViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) ZTListModel *listModel;

@property (nonatomic, strong) NSMutableArray *titleArray;

@property (nonatomic, strong) NSMutableArray *topListArray;

@property (nonatomic, strong) NSMutableArray *imageArray;

@property (nonatomic, strong) UITableView *listTableView;

@end

@implementation ZTListViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.view bringSubviewToFront:self.messageView];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];


#pragma mark - 创建UITableView
    self.listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 22 - 40 - 44) style:UITableViewStyleGrouped];
    [self.view addSubview:self.listTableView];
    self.listTableView.dataSource = self;
    self.listTableView.delegate = self;
    self.listTableView.backgroundColor = [UIColor whiteColor];
    self.listTableView.rowHeight = 120;
    self.listTableView.sectionHeaderHeight = 30;
    // 注册
    [self.listTableView registerClass:[ZTListTableViewCell class] forCellReuseIdentifier:@"listTableCell"];
    
    self.listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.listTableView reloadData];
    [self getData];

}


#pragma mark - 请求数据
-(void)getData
{
    
    
    NSString *urlStr = @"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=ios&device=iphone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.26.2";

    [AppTools getDataWithURL:urlStr Block:^(id result) {
        
        //初始化数组
        self.topListArray = [NSMutableArray array];
        self.titleArray = [NSMutableArray array];
        
        NSMutableArray *dataArray = [result objectForKey:@"datas"];
        
        for (NSMutableDictionary *dic in dataArray) {
            
            [self.titleArray addObject:[dic objectForKey:@"title"]];
            
            NSMutableArray *listArray = [dic objectForKey:@"list"];
            NSMutableArray *array = [NSMutableArray array];
            for (NSMutableDictionary *topDic in listArray) {
                
                ZTListModel *model = [ZTListModel modelWithDictionary:topDic];
                [array addObject:model];
            }
            
            [self.topListArray addObject:array];
        }
        
        // 创建tableHeaderView
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 4)];
        
        NSMutableDictionary *littleDic = [result objectForKey:@"focusImages"];
        NSMutableArray *listArray = [littleDic objectForKey:@"list"];
        for (NSMutableDictionary *dic1 in listArray) {
            
            NSString *pic = [dic1 objectForKey:@"pic"];
            [imageView sd_setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
        }
        self.listTableView.tableHeaderView = imageView;

        [self.listTableView reloadData];
        
    } ErrorBlock:^(NSError *error) {
        
        NSLog(@"error = %@", error);
    }];
    
}



#pragma mark - tableView协议方法
// section数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titleArray.count;
}


// row数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *array = [self.topListArray objectAtIndex:section];
    return array.count;
}


// 创建cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZTListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listTableCell"];
    
    NSMutableArray *array = [self.topListArray objectAtIndex:indexPath.section];
    
    ZTListModel *model = [array objectAtIndex:indexPath.row];
    
    cell.listModel = model;
    
    return cell;
}



// section的表头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];
    arrowImageView.backgroundColor = [UIColor clearColor];
    arrowImageView.image = [UIImage imageNamed:@"findsection_logo@2x.png"];
    [headView addSubview:arrowImageView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(arrowImageView.frame.origin.x + arrowImageView.frame.size.width + 10, arrowImageView.frame.origin.y-5, 200, 20)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text = [self.titleArray objectAtIndex:section];
    [headView addSubview:titleLabel];
    
    
    return headView;
}

// 点击cell触发
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSMutableArray *array = [self.topListArray objectAtIndex:indexPath.section];
    
    ZTListModel *model = [array objectAtIndex:indexPath.row];
    
    ZTListDetailViewController *listDetailVC = [[ZTListDetailViewController alloc] init];
    // 传值
    listDetailVC.key = model.key;
    listDetailVC.contentType = model.contentType;
    listDetailVC.listModel = model;
    
    [(ZHTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:listDetailVC];
    
    [self.navigationController pushViewController:listDetailVC animated:YES];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
